package com.vtech.ont.query;

import com.vtech.ont.base.Consts;
import com.vtech.ont.base.OntPrefixUtil;
import org.apache.jena.vocabulary.OWL2;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
import org.apache.jena.vocabulary.XSD;

/**
 * SPARQL词汇-前缀
 *
 * @author houzhiwei
 * @date 2017/6/29 15:25
 */
public class Vocabulary {

    // [start]** SPARQL Keywords **
    public static final String ASK = "ASK";
    public static final String SELECT = "SELECT";
    public static final String WHERE = "WHERE";
    public static final String CONSTRUCT = "CONSTRUCT";
    public static final String FILTER = "FILTER";
    public static final String BIND = "BIND";
    public static final String GROUP = "GROUP";
    public static final String OPTIONAL = "OPTIONAL";
    public static final String DISTINCT = "DISTINCT";
    /**
     * string ends
     */
    public static final String STRENDS = "STRENDS";
    /**
     * to lower case
     */
    public static final String LCASE = "LCASE";
    /**
     * string
     */
    public static final String STR = "STR";
    // [end]


    // [start]**** Prefixes ****
    public static final String PREFIX_RDF = prefixConcat("rdf", RDF.getURI());
    public static final String PREFIX_RDFS = prefixConcat("rdfs", RDFS.getURI());
    public static final String PREFIX_OWL = prefixConcat("owl", OWL2.getURI());
    public static final String PREFIX_SKOS = prefixConcat("skos", Consts.NS_SKOS);
    public static final String PREFIX_XSD = prefixConcat("xsd", XSD.getURI());
    /**
     * 文本查询
     */
    public static final String PREFIX_TEXT = prefixConcat("text", Consts.NS_JA_TEXT);
    /**
     * ARQ函数
     */
    public static final String PREFIX_AFN = prefixConcat("afn", Consts.NS_AFN);
    /**
     * 都柏林core
     */
    public static final String PREFIX_DC = prefixConcat("dc", Consts.NS_DC);
    // [end]

    /**
     * <pre/>构造SPARQL查询中的前缀
     * eg. "PREFIX dc: &lt;http://purl.org/dc/elements/1.1/&gt; "
     *
     * @param prefix
     * @param ns
     * @return "PREFIX prefix: &lt;ns&gt; "
     */
    public static String prefixConcat(String prefix, String ns)
    {
        StringBuilder builder = new StringBuilder("PREFIX ");
        builder.append(prefix);
        builder.append(": ");
        builder.append("<");
        builder.append(ns);
        builder.append("> ");
        return builder.toString();
    }

    /**
     * 构造SPARQL查询中的前缀
     *
     * @param ns 命名空间
     * @return "PREFIX prefix: &lt;ns&gt; "
     * @date 2016年1月8日下午10:24:49
     */
    public String prefixConcat(String ns)
    {
        String prefix = "";
        prefix = OntPrefixUtil.getPrefixStr(ns);

        StringBuilder builder = new StringBuilder("PREFIX ");
        builder.append(prefix);
        builder.append(":");
        builder.append(" ");
        builder.append("<");
        builder.append(ns);
        builder.append("> ");
        return builder.toString();
    }

    /**
     * 字符串正则匹配 FILTER regex(?var, "pattern")
     *
     * @param var           变量(<b> 无需带 "?" </b>)
     * @param pattern
     * @param caseSensitive 大小写敏感
     * @return
     */
    public static String filterRegexConcat(String var, String pattern, boolean caseSensitive)
    {
        StringBuilder builder = new StringBuilder("FILTER regex(");
        builder.append("?");
        builder.append(var);
        builder.append(", \"");
        builder.append(pattern);
        if (!caseSensitive) {
            builder.append("\", ");
            builder.append("i");
        }
        builder.append(")");
        return builder.toString();
    }
}
